<!-- 公司表单弹窗 -->
<form id="company-form" lay-filter="company-form" class="layui-form model-form">
    <input name="id" type="hidden"/>
    <div class="layui-form-item">
        <label class="layui-form-label">所属公司</label>
        <div class="layui-input-block">
            <select name="pid" xm-select="company" xm-select-radio xm-select-skin="normal" lay-verify="required">
                <option value="">请输入所属公司</option>
            </select>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">名称</label>
        <div class="layui-input-block">
            <input name="name" placeholder="请输入名称" type="text" class="layui-input" minlength="6" maxlength="100"
                   lay-verify="required" required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">备注</label>
        <div class="layui-input-block">
            <textarea name="remark" placeholder="请输入备注" class="layui-textarea" maxlength="255" lay-verify="required"
                      required></textarea>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">排序号</label>
        <div class="layui-input-block">
            <input name="sort" placeholder="请输入排序号" type="number" class="layui-input"/>
        </div>
    </div>
    <div class="layui-form-item model-form-footer">
        <button class="layui-btn layui-btn-primary close" ew-event="closeDialog" type="button">取消</button>
        <button class="layui-btn" lay-filter="company-form-submit" lay-submit>保存</button>
    </div>
</form>
<script>
    layui.use(['layer', 'crab', 'form', 'formSelects'], function () {
        var layer = layui.layer;
        var crab = layui.crab;
        var form = layui.form;
        var formSelects = layui.formSelects;
        var edit = false;

        form.render('radio');

        // 编辑的参数信息
        var company = crab.getFormData();
        if (company) {
            edit = true;
            // 回显参数数据
            form.val('company-form', company);
        }

        // 所属部门
        crab.get('/sys/company/list_effective', {}, function (data) {
            var l = data.length;
            if (l > 0) {
                var companyData = new Array();
                    companyData.push({name: '无', value: 0});
                for (var i = 0; i < data.length; i++) {
                    companyData.push({name: data[i].name, value: data[i].id});
                }
                formSelects.data('company', 'local', {arr: companyData});
                if (company) {
                    formSelects.value('company', [company.pid])
                }
            }
        });

        // 表单提交事件
        form.on('submit(company-form-submit)', function (data) {
            crab.request('/sys/company', data.field, function () {
                layer.msg(edit ? '修改成功' : '添加成功', {icon: 1});
                crab.finishPopupCenter();
            }, edit ? 'PUT' : 'POST');
            return false;
        });
    });
</script>